{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 全景图片的制作方法\n",
    "\n",
    "<table width=\"100%\" border=\"0\">\n",
    "<tr>\n",
    "  <td valign=\"top\"  width=\"40%\">\n",
    "    <img src=\"images/cubemap/in_6x1.jpg\" align=\"left\"/>\n",
    "  </td>\n",
    "  <td valign=\"top\"  width=\"10%\">\n",
    "    <img src=\"images/cubemap/arrow.png\" align=\"left\"/>\n",
    "  </td>\n",
    "  <td valign=\"top\"  width=\"50%\">\n",
    "    <img src=\"images/cubemap/panoramic.jpg\" align=\"right\"/>\n",
    "  </td>\n",
    "</tr>\n",
    "</table>\n",
    "\n",
    "<font style=\"color:red;font-size:20px\">原理就是：将六个方向(右、左、上、下、前、后)的图像合成为一张全景图片</font>\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## 1、导入包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "from img_utils import show_img  # 显示图片\n",
    "import os"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## 2、提供样例图片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "input_dir = \"images/cubemap\"  # 存放样例图片的路径\n",
    "# 查看 images/cubemap 目录下的样例图片\n",
    "input_images =  list(filter(lambda f: f.startswith(\"in_\") and not f.__contains__(\"_6x1\"), os.listdir(input_dir)))\n",
    "print(\"样例图片：\\n\" , input_images)\n",
    "for input_img in input_images:\n",
    "    show_img( os.path.join(input_dir, input_img), title=input_img, figsize=(4, 3))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "### 不管六个方向图像的位置怎样，我们都要统一转换为如下： 6x1 的方体贴图纹理图片\n",
    "\n",
    "<table width=\"100%\" border=\"0\">\n",
    "<tr>\n",
    "  <td valign=\"top\"  width=\"40%\">\n",
    "    <img src=\"images/cubemap/in_02.jpg\" align=\"left\"/>\n",
    "  </td>\n",
    "  <td valign=\"top\"  width=\"10%\">\n",
    "    <img src=\"images/cubemap/arrow.png\" align=\"left\"/>\n",
    "  </td>\n",
    "  <td valign=\"top\" width=\"50%\"  align=\"middle\">\n",
    "    <img src=\"images/cubemap/in_6x1.jpg\" />\n",
    "    <font style=\"font-size:29px;\">&nbsp;右 &nbsp;&nbsp;&nbsp;&nbsp;\n",
    "        左 &nbsp;&nbsp;&nbsp; &nbsp;\n",
    "        上 &nbsp;&nbsp;&nbsp;&nbsp;\n",
    "        下 &nbsp;&nbsp;&nbsp;&nbsp;\n",
    "        前 &nbsp;&nbsp;&nbsp;&nbsp;\n",
    "        后 &nbsp;&nbsp;&nbsp;</font>\n",
    "  </td>\n",
    "</tr>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## 3、选择一张样例图片作为输入图像"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "# 从上面样例图片中，选择一张样例图片作为输入图像\n",
    "sample_image = 'in_02.jpg'\n",
    "\n",
    "# 构建样例图片的路径\n",
    "input_image = os.path.join(input_dir, sample_image)\n",
    "print(\"原始图片路径：\", os.path.normpath(input_image))\n",
    "\n",
    "# 显示样例的原始图片\n",
    "show_img(input_image, title='原始图片', figsize=(4.8, 3.6))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "### 第一种布局的图像位置和行列号\n",
    "<table width=\"100%\" border=\"0\">\n",
    "<tr>\n",
    "  <td valign=\"top\"  width=\"40%\">\n",
    "<img src=\"images/cubemap/cubemap_intro.jpg\" style=\"width:100%\" /> \n",
    "  </td>\n",
    "   <td valign=\"top\" style=\"text-align:left;font-size:16px;font-family:Verdana\" width=\"40%\">\n",
    "      <font color=\"black\">rludfb为（右、左、上、下、前、后）6个方位的首字母，<br>按顺序排列的行列号如下： <br> <br></font>\n",
    "      <font color=\"red\">rludfb = [(3, 2), (1, 2), (2, 1), (2, 3), (2, 2), (4, 2)] </font>\n",
    "   </td>\n",
    "</tr>\n",
    "</table>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## 4、统一转换为 6x1 的方体贴图纹理（cubemap）图片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "# 指定 rludfb（右左上下前后）顺序的 行列号 列表\n",
    "rludfb=[(3, 2), (1, 2), (2, 1), (2, 3), (2, 2), (4, 2)] # sample_image = in_01 ~ in_06\n",
    "# rludfb=[(1, 2), (4, 2), (1, 1), (1, 3), (1, 2), (3, 2)] # sample_image = in_07 \n",
    "# rludfb=[(4, 2), (2, 2), (3, 1), (3, 3), (3, 2), (1, 2)] # sample_image = in_08 \n",
    "# rludfb=[(1, 2), (3, 2), (4, 1), (4, 3), (4, 2), (2, 2)] # sample_image = in_09 \n",
    "\n",
    "from panorama import conv_to_cubemap_6x1\n",
    "# 转换为 cubemap 6x1 的图片\n",
    "cubemap_6x1_img = conv_to_cubemap_6x1(input_image, rludfb=rludfb)\n",
    "print(\"cubemap 6x1图片路径：\", os.path.normpath(cubemap_6x1_img))\n",
    "\n",
    "# 显示转换后，CubeMap 6x1图片\n",
    "show_img(cubemap_6x1_img, title='CubeMap 6x1')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## 5、制作全景图片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "from panorama import cubemap_6x1_to_panorama\n",
    "\n",
    "# CubeMap 6x1图片 转 全景图片\n",
    "panoramic_image = cubemap_6x1_to_panorama(cubemap_6x1_img, 'output/out_panoramic.jpg')\n",
    "print(\"360全景图片路径：\", os.path.normpath(panoramic_image))\n",
    "\n",
    "# 显示转换后的全景图片\n",
    "show_img(panoramic_image, title='360全景图片')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## 6、用支持全景的软件查看\n",
    "\n",
    "<font style=\"font-size:20px;\">比如：PotPlayer、utovr、GoPro VR Player、Insta360 Player</font>\n",
    "<br><br>\n",
    "<font style=\"font-size:20px;\" color=\"red\">请搜索关键字“cubemap 全景”，了解更多\n",
    "    </font>"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
